package com.hadwinling.startorder;

/**
 * @Classname RealTimeStartOrdder
 * @Description TODO
 * @Date 2021/7/12 9:57
 * @Created by
 */

import cn.hutool.core.io.file.FileReader;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 定时
 * 继承Application接口后项目启动时会按照执行顺序执行run方法
 * 通过设置Order的value来指定执行的顺序
 */
@Component
@Order(value = 2)
public class RealTimeStartOrder implements ApplicationRunner {
    @Autowired
    private Session iotdbSession;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        /*
//        FileReader fileReader = new FileReader("D:\\MySoftWare\\PCWare\\Tencent\\AppCache\\WetChatCache\\WeChat Files\\wxid_ysw3yj1tpdwp22\\FileStorage\\File\\2021-06\\go30");
        FileReader fileReader = new FileReader("/home/hedian/go30");

        List<String> lines = fileReader.readLines();
        int line = 0;
        long timestamp = System.currentTimeMillis();

        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            measurements.add("s" + (i + 1));
            types.add(TSDataType.FLOAT);
        }

        while (true) {
            if (line == lines.size()) {
                line = 0;
            }
            for (int rhb = 0; rhb < 18; rhb++) {

                for (int zb = 0; zb < 6; zb++) {// 6 个指标

                    for (int shike = 0; shike < 98; shike++) { // 98 个时刻

                        for (int zj = 0; zj < 52; zj++) { // 52 个组件-

                            String deviceId =   "root.dc_dx2." + shujukuaiArray[zb] + ".zj" + zj;
//                            System.out.println(deviceId);
                            List<Object> values = new ArrayList<>();
                            final String[] s = lines.get(line).split(" ");
                            for (int i = 0; i < 32; i++) {
                                String value = s[i];
                                values.add(Float.parseFloat(value));
                            }
                            line++;
                            deviceIds.add(deviceId);
                            valuesList.add(values);
                            measurementsList.add(measurements);
                            typesList.add(types);
                            timestamps.add(timestamp);
                        }
                        try {
                            iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
                        } catch (IoTDBConnectionException e) {
                            e.printStackTrace();
                        } catch (StatementExecutionException e) {
                            e.printStackTrace();
                        }

                        deviceIds.clear();
                        measurementsList.clear();
                        valuesList.clear();
                        timestamps.clear();

                        timestamp = timestamp + 1000; // 1秒一个时刻
                        Thread.sleep(1000);
                    }
                }

            }
        }*/
    }
}
